
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// struct hostent *gethostbyname (const char *hostname);

int main(int argc, char **argv) {
	int i;
	struct hostent *he;
	struct in_addr **addr_list;
	char * site = "www.baidu.com";
	he = gethostbyname(site);

	if (he == NULL) {
		printf("gethostbyname() failed\n");
		return 1;
	}

	// print information about this host:
	printf("host name    : %s\n", site);
	printf("official name: %s\n", he->h_name);
	printf("IP addresses :\n");
	addr_list = (struct in_addr **)he->h_addr_list;
	for (i = 0; addr_list[i] != NULL; i++) {
		printf("\t%s\n", inet_ntoa(*addr_list[i]));
	}
	printf("\n");


	char* ip_abc =  "64.233.162.86"; //  "127.0.0.1"; //
	struct in_addr addr;
	inet_aton(ip_abc, &addr);
	he = gethostbyaddr(&addr, sizeof(addr), AF_INET);
	if (he == NULL) {
		printf("gethostbyaddr() failed\n");
		return 1;
	}
	printf("Host name: %s\n", he->h_name);

	return 0;
}